﻿using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace SopCastRecorder
{
	public class SopCastSaveDialog : SopCastBase
	{
		private static int InputTextIndex = OptionFile.Option1.SopCastSaveDialogInputTextIndex;

		private IntPtr InputTextHandle
		{
			get
			{
				return this.EditHandles[InputTextIndex];
			}
		}
		public string InputText
		{
			set
			{
				Win32Api.SetText(this.InputTextHandle, value);
			}
		}

		public static SopCastSaveDialog GetInstance(IntPtr[] hWnds)
		{
			for (var i = 0; i < hWnds.Length; i++)
			{
				var sopCastDialog1 = GetInstance2(hWnds[i]);
				if (sopCastDialog1 != null)
				{
					return sopCastDialog1;
				}
			}
			return null;
		}
		private static SopCastSaveDialog GetInstance2(IntPtr hWnd)
		{
			var dialog = new SopCastSaveDialog()
			{
				MainHandle = hWnd
			};

			if (!dialog.CheckVisible())
			{
				return null;
			}

			if (!dialog.CheckCaption())
			{
				return null;
			}

			dialog.RefreshHandle();

			if (!dialog.CheckHandle())
			{
				return null;
			}
			return dialog;
		}
		protected override bool CheckCaption()
		{
			var s = Win32Api.GetWindowTextCS(this.MainHandle);
			if (Regex.IsMatch(s, OptionFile.Option1.DialogCaptionRegex))
			{
				return true;
			}
			return false;
		}
		protected override bool CheckHandle()
		{
			if (this.EditHandles.Count() <= InputTextIndex)
			{
				return false;
			}
			return true;
		}

		public void ClickOkButton()
		{
			Win32Api.Return(this.InputTextHandle);
		}
	}
}
